<template>
  <div>
    <el-card>
      <el-table border style="margin: 10px 0" :data="skuArr">
        <el-table-column
          label="序号"
          width="100px"
          align="center"
          type="index"
        ></el-table-column>
        <el-table-column
          label="名称"
          width="150px"
          prop="skuName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="描述"
          width="150px"
          prop="skuDesc"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="图片" width="150px">
          <template #="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column
          label="重量"
          width="150px"
          prop="weight"
        ></el-table-column>
        <el-table-column
          label="价格"
          width="150px"
          prop="price"
        ></el-table-column>
        <el-table-column label="操作" width="250px" fixed="right">
          <template #="{ row }">
            <el-button
              :type="row.isSale == 1 ? 'success' : 'info'"
              size="small"
              :icon="row.isSale == 1 ?'Top' : 'Bottom'"
              @click="updateDelist(row)"
            ></el-button>
            <el-button type="primary" size="small" icon="Edit" @click="editSku"></el-button>

            <el-button type="danger" size="small" icon="Delete" @click="deleteSku(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10]"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="getSkuArr"
        @size-change="changeSize"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { reqGetSkuArr, reqListing, reqDelist ,reqDeleteSku} from '@/api/product/sku'
import { ElMessage } from 'element-plus'
let pageNo = ref(1)
let pageSize = ref(5)
let total = ref(0)
let skuArr = ref([])
onMounted(() => {
  getSkuArr()
})
const getSkuArr = (pager = 1) => {
  pageNo.value = pager
  reqGetSkuArr(pageNo.value, pageSize.value).then((res) => {
    if (res.code == 200) {
      total.value = res.data.total
      skuArr.value = res.data.records
    }
  })
}
const deleteSku = (row:any) => {
  reqDeleteSku(row.id).then((res) => {
    if (res.code == 200) {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      getSkuArr(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
    }
  })
}
const editSku = () => {
  ElMessage({
        type: 'success',
        message: '努力更新中...',
      })
}
const changeSize = () => {
  getSkuArr()
}
const updateDelist = (row: any) => {
  if (row.isSale == 1) {
    reqDelist(row.id).then((res) => {
      if (res.code == 200) {
        ElMessage({
          type: 'success',
          message: '下架成功',
        })
        getSkuArr()
      }
    })
  } else {
    reqListing(row.id).then((res) => {
      if (res.code == 200) {
        ElMessage({
          type: 'success',
          message: '上架成功',
        })
        getSkuArr()
      }
    })
  }
}
</script>
<script lang="ts">
export default {
  name: 'SKU',
}
</script>

<style lang="scss"></style>
